package com.ruoyi.activity.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.activity.entity.ActivityRankConfig;
import com.ruoyi.activity.entity.RankConfigVO;
import com.ruoyi.activity.mapper.ActivityRankConfigDao;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DataSource(DataSourceType.SLAVE)
public class ActivityRankService {
    @Resource
    private ActivityRankConfigDao activityRankConfigDao;

    public Page<ActivityRankConfig> getActivityRankConfigList(Integer type, int pageIndex, int pageSize){
        Page<ActivityRankConfig> page = new Page<>(pageIndex,pageSize);

        LambdaQueryWrapper<ActivityRankConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (type != null){
            queryWrapper.eq(ActivityRankConfig::getType,type);
        }
        return activityRankConfigDao.selectPage(page, queryWrapper);
    }

    public ActivityRankConfig getOneActivity(Integer id){
        return activityRankConfigDao.selectById(id);
    }

    public void updateActivity(RankConfigVO configVO){
        ActivityRankConfig config = activityRankConfigDao.selectById(configVO.getId());
        config.setType(configVO.getActivityType());
        config.setActivityStartTime(DateUtil.parse(configVO.getStart(), DatePattern.NORM_DATETIME_PATTERN));
        config.setActivityEndTime(DateUtil.parse(configVO.getEnd(), DatePattern.NORM_DATETIME_PATTERN));
        config.setDescption(configVO.getDescription());
        config.setGiftId(configVO.getGiftId());
        config.setUpdateTime(null);
        activityRankConfigDao.updateById(config);
    }

    public void addActivity(RankConfigVO configVO){
        ActivityRankConfig config = new ActivityRankConfig();
        config.setType(configVO.getActivityType());
        config.setActivityStartTime(DateUtil.parse(configVO.getStart(), DatePattern.NORM_DATETIME_PATTERN));
        config.setActivityEndTime(DateUtil.parse(configVO.getEnd(), DatePattern.NORM_DATETIME_PATTERN));
        config.setDescption(configVO.getDescription());
        config.setGiftId(configVO.getGiftId());
        activityRankConfigDao.insert(config);
    }

    public void deleteActivity(Integer id){
        activityRankConfigDao.deleteById(id);
    }

    public List<Map<String,Object>> getLabels(){
        List<ActivityRankConfig> list = activityRankConfigDao.selectList(new QueryWrapper<>());

        List<Map<String, Object>> result = new ArrayList<>();
        list.forEach(e->{
            Map<String, Object> map = new HashMap<>(4);
            map.put("label",e.getDescption());
            map.put("value",e.getType());
            result.add(map);
        });
        return result;
    }
}